import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:material_collection_app/utils/dio_utils.dart';
import 'package:material_collection_app/utils/utils.dart';
import 'package:material_collection_app/widget/common/top_bar.dart';

class RfidDetailPage extends StatefulWidget  {
  final Map arguments;
  RfidDetailPage({Key key, this.arguments}) : super(key: key);

  @override
  State<StatefulWidget> createState() => MyState();
}

class MyState extends State<RfidDetailPage> {
  Map detail;
  bool loadSuccess;

  @override
  void initState() {
    super.initState();
    _loadData();
  }

  // 加载数据
  void _loadData() async {
    var rfid = widget.arguments['rfid']; 
    loadSuccess = false;

    DioUtils.request("goods/getDetailsByrfid",
      context: context,
      method: "POST",
      params: {
        'rfid': rfid,
      },
      onSuccess: (res) {
        if(res['code'] == 1) {
          var data = res["data"];

          setState(() {
            loadSuccess = true;
          });

          if(data is Map) {
            setState(() {
              detail = data;
            });
          }
        }else {
          Utils.showText(text: res['msg']);
        }
      },
      onError: (error) {}
    );
  }

  Widget _buildCardHead(title) {
    return Container(
      width: 680.w,
      height: 90.w,
      alignment: Alignment.centerLeft,
      decoration: BoxDecoration(
        border: Border(bottom:BorderSide(width: 1,color: Color(0xFFF1F1F1)))
      ),
      child: Text(title,
        style: TextStyle(
          color: Color(0xFF333333), fontSize: 16, fontWeight: FontWeight.bold
        ),
      ),
    );
  }


  Widget _buildRowText(title, text, [Color color]) {
    return Padding(
      padding: EdgeInsets.fromLTRB(0, 15.w, 0, 15.w),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: <Widget>[
          Text(title.toString(), style: TextStyle(
            color: Color(0xFF999999),
            fontSize: 14
          )),
          Container(
            width: 500.w,
            alignment: Alignment.centerRight,
            child: Text(text.toString(), maxLines: 1, overflow: TextOverflow.ellipsis, style: TextStyle(
              color: color ?? Color(0xFF333333),
              fontSize: 14,
              
            )),
          )
        ],
      ),
    );
  }

  Widget _buildRowPicture() {
    List<Widget> images = [];
    List<String> imgList = [];

    for(var i = 0; i<detail['enter_files'].length; i++) {
      var item = detail['enter_files'][i];
      
      imgList.add(DioUtils.API_PREFIX+item['file_path']);

      images.add(
        GestureDetector(
          onTap: () {
            Navigator.pushNamed(
              context, "/photo_view", 
              arguments: {'list': imgList, 'index': i}
            );
          },
          child: Container(
            width: 100.w,
            height: 100.w,
            decoration: BoxDecoration(
              color: Utils.hexToColor('#F1F3F4'),
              borderRadius: BorderRadius.circular(4.w),
            ),
            margin: EdgeInsets.only(right: 10.w),
            child: ClipRRect(
              borderRadius: BorderRadius.circular(4.w),
              child: _buildNetworkImgOrEmptyImg(item['file_path']),
            )
          )
        ),
      );
    }

    return Padding(
      padding: EdgeInsets.fromLTRB(0, 15.w, 0, 15.w),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: <Widget>[
          Text('图片', style: TextStyle(
            color: Color(0xFF999999),
            fontSize: 14
          )),
          Container(
            width: 500.w,
            child: Wrap(
              spacing: 10.w,
              runSpacing: 25.w,
              alignment: WrapAlignment.end,
              children: images
            )
          )
        ]
      )
    );
  }

  Widget _buildNetworkImgOrEmptyImg(path) {
    if(path != '') {
      return Image.network(DioUtils.API_PREFIX + path, 
        fit: BoxFit.cover,
      );
    }else {
      return Image.asset('/assets/no_img.jpg', fit: BoxFit.cover);
    }
  }

  Widget _buildPageOne() {
    if(loadSuccess) {
      if(detail is Map && detail.isNotEmpty) {
        return SingleChildScrollView(
          padding: EdgeInsets.fromLTRB(25.w, 0.w, 25.w, 60.w),
          child: Container(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                Container(
                  width: 700.w,
                  margin: EdgeInsets.only(top: 24.w),
                  padding: EdgeInsets.only(bottom: 34.w),
                  decoration: BoxDecoration(
                    color: Colors.white,
                    borderRadius: BorderRadius.circular(20.w),
                  ),
                  child: Column(
                    children: <Widget>[
                      _buildCardHead('基本信息'),
                      Container(
                        padding: EdgeInsets.fromLTRB(24.w, 20.w, 24.w, 0),
                        child: Column(
                          children: <Widget>[
                            _buildRowText('入库单号', detail['entry_order_no']),
                            _buildRowText('物资编号', detail['code']),
                            _buildRowText('物资名称', detail['goods_name']),
                            _buildRowText('入库数量', detail['enter_qty']),
                            _buildRowText('在库数量', detail['real_qty']),
                            _buildRowText('规格/型号', detail['goods_model']),
                            _buildRowText('物资分类', detail['type_name'] != null ? detail['type_name'] : '--'),
                            _buildRowText('计量单位', detail['goods_unit']),
                            _buildRowText('产品估价', detail['goods_price'] != null ? detail['goods_price'] : '--'),
                            _buildRowText('有效期', detail['valid_time']),
                            _buildRowText('来源单位', detail['goods_source']),
                            _buildRowText('所属仓库', detail['store_name']),
                            _buildRowText('所属库区', detail['store_library_name']),
                            _buildRowText('所属库位', detail['store_detail_code'] != null ? detail['store_detail_code'] : '--'),
                            _buildRowText('RFID编码', detail['rfid_code']),
                            _buildRowText('备注', detail['remark']),
                            _buildRowPicture(),
                          ],
                        ),
                      )
                    ],
                  ),
                )
              ],
            ),
          )
        );
      }else {
        return Container(
          child: Center(
            child: SizedBox(
              height: 300,
              child: Column(
                children: <Widget>[
                  Image.asset('assets/rfid_none.png', width: 400.w),
                  Container(
                    margin: EdgeInsets.only(top: 40),
                    child: Text('此RFID标签不属于系统生成的RFID标签。', style: TextStyle(
                      color: Utils.hexToColor('#515a6e'),
                      fontSize: 14
                    )),
                  ),
                  Container(
                    margin: EdgeInsets.only(top: 5),
                    child: Text('请选择系统生成的RFID标签。', style: TextStyle(
                      color: Utils.hexToColor('#515a6e'),
                      fontSize: 14
                    )),
                  )
                ],
              ),
            )
          ),
        );
      }
    }else {
      return Container();
    }
  }

  @override
  Widget build(BuildContext context) {
    ScreenUtil.init(context, width: 750, height: 1334, allowFontScaling: false);

    return DefaultTabController(
      length: 3,
      initialIndex: 0,
      child: Scaffold(
        appBar: TopBar(
          title: '物资详情',
          backgroundColor: Utils.hexToColor('#297CFF'),
        ),
        body: _buildPageOne()
      )
    );
  }
}